<?
	require_once('../inc.php');
	if(isset( $_REQUEST['id']) && $_REQUEST['id'] != '' )
	{
		$rs = GetContactMethod($_REQUEST['id']);
		if($rs)
		{
			$model = InitContactMethod($rs);
		}
	}
	
	
	if(isset($_REQUEST['IsPostBack']) && $_REQUEST['IsPostBack'] == 'true')
	{
		$ValidationErrors = ValidateContactMethod($_REQUEST);
		if($ValidationErrors == null)
		{
			try{
				ContactMethod::Save($_REQUEST);
				header('location: ../Users/details.php?id=' . $_REQUEST['UserId']);
				die;
			}catch(Exception $ex){
				?><div class="error"><?=$ex->getMessage()?></div> <?
			}
		}
		$model = InitContactMethod($_REQUEST);
	}
	
	if(!isset($model)) $model = new ContactMethod();
	
	$users = User::GetAll();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit: <?=$user->FirstName?></title>
    <style type="text/css">
		.error {
			color: red;
		}
		.iError {
			border-color: red;
			background-color: #FEE;
		}
		form div {
			margin: 2px 2px 2px 2px;
			clear:both;
		}
		label {
			display: block;
			width: 100px;
			text-align: right;
			float: left;
			margin-right: 5px;
		}
	</style>
</head>

<body>
	<form method="post">
    	<ul class="error">
    	<? foreach($ValidationErrors as $key => $value) { ?>
        	<li><?=$key?>: <?=$value?> </li>
        <? } ?>
    	</ul>
    	<input type="hidden" name="id" value="<?=$model->id?>" />
    	<input type="hidden" name="IsPostBack" value="true" />        
        <div>
            <label for="UserId">UserId:</label>
            <select name="UserId" size="1">
            	<? while($rs = $users->fetch_assoc()){ ?>
            		<option value="<?=$rs['id']?>" <?= $rs['id']== $model->UserId ? 'selected="selected"' : 'gg'?> >
						<?=$rs['FirstName']?> <?=$rs['LastName']?>
                    </option>
                <? } ?>
            </select>
        </div>
        <div>
            <label for="UserId">Type:</label>
            <select name="TypeId" size="1">
            		<option value="1" <?= 1 == $model->TypeId ? 'selected="selected"' : 'gg'?> >
						Email
                    </option>
            		<option value="2" <?= 2 == $model->TypeId ? 'selected="selected"' : 'gg'?> >
						Phone
                    </option>
            </select>
        </div>
        <div>
            <label for="Value">Value:</label>
            <input
            	class="<?=isset($ValidationErrors['Value']) ? 'iError' : ''?>"
            	type="text" name="Value"
            	value="<?=$model->Value?>" id="Value" />
            <? if(isset($ValidationErrors['Value'])) { ?><?=$ValidationErrors['Value']?><? } ?>
        </div>
        <input type="submit" value="Save" />
    </form>
</body>
</html>